<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link th:href="@{/css/font.css}" rel="stylesheet"/>
    <link th:href="@{/css/xadmin.css}" rel="stylesheet"/>
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/js/xadmin.js}" type="text/javascript" charset="utf-8"></script>
    <script th:inline="javascript"> var ctx = [[@{/}]];</script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">用户名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="userName" id="userName" placeholder="请输入用户名" autocomplete="off"
                                           class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">创建时间</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" id="createTime" placeholder="请选择时间" autocomplete="off">
                                    </div>
                                </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">手机</label>
                                <div class="layui-input-inline">
                                    <input type="tel" name="phonenumber" id="phonenumber" placeholder="请输入手机号"  autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">邮箱</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="email" id="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">登录名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="loginName" id="loginName" placeholder="请输入登录名" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <a class="layui-btn" lay-submit="" lay-filter="sreach" id="sreach"><i
                                            class="layui-icon">&#xe615;</i></a>
                                </div>

                        </div>

                    </form>
                </div>

                <div class="layui-card-body ">
                    <!--<script type="text/html" id="toolbarDemo">-->
                        <!--<div class="layui-btn-container">-->
                            <!--<button class="layui-btn layui-btn-sm" lay-event="addUser">添加用户</button>-->
                        <!--</div>-->
                    <!--</script>-->
                    <table id="demo" class="layui-table layui-form" lay-filter="rowTest"></table>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="resetPwd">重置</a>
                    </script>
                </div>


            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.config({
        version: '1559794877226' //为了更新 js 缓存，可忽略
    });
    layui.use(['laydate', 'layer', 'table'], function () {
        var laydate = layui.laydate //日期
            ,
            layer = layui.layer //弹层
            ,
            table = layui.table //表格

        //日期范围
        laydate.render({
            elem: '#createTime'
            ,range: '~'
            ,done: function(value, date, endDate){
            if(date.year == undefined){
                startTime = '';
                endTime = '';
            }else{
                startTime = date.year+'-'+date.month+'-'+date.date;
	            endTime = endDate.year+'-'+endDate.month+'-'+endDate.date;
            }
	      }

            });
        //加载表格
        table.render({
            elem: '#demo',
            height: 312,
            url: '/getUserList'
            ,toolbar: 'default'
            ,id: 'testReload'
            ,page: true
            //,totalRow: true
            ,cols: [
                [{
                    checkbox: true
                }, {
                    field: 'userId',
                    title: '用户ID'
                }, {
                    field: 'loginName',
                    title: '登录名称',
                    sort: true
                }, {
                    field: 'userName',
                    title: '用户名称'
                }, {
                    field: 'email',
                    title: '邮箱',
                    visible: false
                }, {
                    field: 'phonenumber',
                    title: '手机'
                }, {
                    field: 'createTime',
                    title: '创建时间',
                     width: 165,
                    sortable: true
                }, {
                    title: '操作',
                    align: 'center',
                    width: 165,
                    toolbar: "#barDemo"
                }]
            ]
        });

          $('#sreach').on('click', function () {
            // 搜索条件
            var loginName = $('#loginName').val();
            var userName = $('#userName').val();
            var createTime = $('#createTime').val();
            var email = $('#email').val();
            var phonenumber = $('#phonenumber').val();
            if(typeof startTime == "undefined"){
                startTime = '';
                endTime = '';
            }

            table.reload('testReload', {
                method: 'post',
                where: {
                    'userName': userName,
                    'loginName':loginName,
                    'params[beginTime]':startTime,
                    'params[endTime]':endTime,
                    //'createTime':createTime,
                    'email':email,
                    'phonenumber':phonenumber
                }
                , page: {
                    curr: 1
                }
            });
        });


        //监听行工具事件
        table.on('tool(rowTest)', function (obj) {
            var data = obj.data //获得当前行数据
                ,
                layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'resetPwd') {
                layer.msg('重置密码');
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    //layer.close(index);
                    console.log(data.userId);
                    //向服务端发送删除指令
                     $.ajax({   
                url:ctx+'deleteUser',       
                method:'post',       
                data:{'ids':data.userId},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                //layer.msg('删除成功');
                                layer.alert("删除成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  

                });
            } else if (layEvent === 'edit') {
                //layer.msg('编辑操作');
                var userId = data.userId;
                console.log(userId);
                xadmin.open('编辑用户',`/editUserPage/${userId}`,700,500);
            }
        });



  //监听头工具栏事件
  table.on('toolbar(rowTest)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id)
    ,data = checkStatus.data; //获取选中的数据
    switch(obj.event){
      case 'add':
        //layer.msg('添加');
        xadmin.open('添加用户','/addUserPage',700,500);
      break;
      case 'update':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else if(data.length > 1){
          layer.msg('只能同时编辑一个');
        } else {
          var userId = data[0].userId;
          console.log(userId);
          xadmin.open('编辑用户',`/editUserPage/${userId}`,700,500);
        }
      break;
      case 'delete':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else {
            //console.log(data);
            layer.confirm('确认要删除选中的' + data.length + '条数据吗?', function(index){

                console.log(data)
                var arrId = [];
                data.forEach(function(currentValue){
                    arrId.push(currentValue.userId);
                });
                var ids = arrId.join(",")
                console.log(ids);

                $.ajax({   
                url:ctx+'deleteUser',       
                method:'post',       
                data:{'ids':ids},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                //layer.msg('删除成功');
                                layer.alert("删除成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  

            });
        }
      break;
    };
  });



    });
</script>

</html>